import {isReactive, isRef, toRef, toRefs} from "vue";
import {ElMessage} from "element-plus";
import axios from "~/config/axios.js";
import {chatTabRef, newCasId} from "~/config/state.js";
import router from "~/config/router.js";
import {nextTick} from "vue";
import {useRoute} from "vue-router";

export function localGet(key) {
    const value = window.localStorage.getItem(key)
    try {
        return JSON.parse(window.localStorage.getItem(key))
    } catch (error) {
        return value
    }
}

export function localSet(key, value) {
    window.localStorage.setItem(key, JSON.stringify(value))
}

export function localRemove(key) {
    window.localStorage.removeItem(key)
}

export function dateToTimeStr(date) {
    if (date == null) {
        return undefined
    }
    const H = date.getHours();
    const m = date.getMinutes();
    const s = date.getSeconds();
    return (
        (H < 10 ? "0" + H : H) +
        ":" +
        (m < 10 ? "0" + m : m) +
        ":" +
        (s < 10 ? "0" + s : s)
    );
}

export function timeStrToDate(timeStr) {
    if (typeof timeStr != 'string') {
        return undefined
    }
    const hms = timeStr.split(':')
    const date = new Date();
    date.setHours(hms[0])
    date.setMinutes(hms[1])
    date.setSeconds(hms[2])
    return date
}


export function cityToStr(city) {
    if (city?.cityId != undefined) {
        return city.provinceName + '--' + city.cityName
    }
    return null
}

export function getDeepPropValue(obj, path) {
    const propPaths = path.split(".")
    let t = null;
    for (let i = 0; i < propPaths.length; i++) {
        t = obj[propPaths[i]];
    }
    return t
}

export function getDeepPropRef(obj, path) {
    const propPaths = path.split(".")
    let tempObj = obj
    let t = undefined
    // for (let i = 1; i < propPaths.length; i++) {
    //     t = toRef(t.value, propPaths[i])
    // }
    // console.log(t)
    for (let i = 0; i < propPaths.length; i++) {
        if (isReactive(tempObj[propPaths[i]])) {
            t = tempObj[propPaths[i]];
            tempObj = t
        } else if (isRef(tempObj)) {
            if (tempObj.value instanceof Object) {
                t = toRef(tempObj.value, propPaths[i])
                tempObj = t.value
            } else {
                throw 'path error'
            }
        } else {
            t = toRef(tempObj, propPaths[i])
            tempObj = t.value
        }
    }

    return t
}

Date.prototype.format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        S: this.getMilliseconds(), //毫秒
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(
            RegExp.$1,
            (this.getFullYear() + "").substr(4 - RegExp.$1.length)
        );
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(
                RegExp.$1,
                RegExp.$1.length == 1
                    ? o[k]
                    : ("00" + o[k]).substr(("" + o[k]).length)
            );
        }
    }
    return fmt;
};

export function dateStrToDate(dateStr) {
    if (typeof dateStr == 'string') {
        const ymd = dateStr.split("-");
        const date = new Date();
        date.setFullYear(ymd[0])
        date.setMonth(ymd[1] - 1) // ..... just....
        date.setDate(ymd[2])
        return date;
    }
    return undefined
}


export function showSuccessAndReload() {
    ElMessage.success("操作成功")
    setTimeout(() => {
        location.reload()
    }, 1500)
}

export function getRole(path) {
    if (path.indexOf("admin") != -1) {
        return "admin"
    } else if (path.indexOf("supplier") != -1) {
        return "supplier"
    } else if (path.indexOf("user") != -1) {
        return "consumer"
    }
}

export function getProductType(path){
    if (path.indexOf("traffic") != -1) {
        return "traffic"
    } else if (path.indexOf("flight") != -1) {
        return "flight"
    } else if (path.indexOf("scenic") != -1) {
        return "scenic"
    } else if (path.indexOf("hotel")!=-1){
        return "hotel"
    }
}

export function getRolePrefix(path) {
    if (path.indexOf("admin") != -1) {
        return "admin"
    } else if (path.indexOf("supplier") != -1) {
        return "supplier"
    } else if (path.indexOf("user") != -1) {
        return "user"
    }
}

export const addNewContact = async (role, id) => {
    // const prefix = getRolePrefix(router.currentRoute.value.path)
    // await router.push(`/${prefix}/chat`)
    const casId = await axios.get(`/getCasId/${role}/${id}`);
    newCasId.value = casId
    chatTabRef.value.$el.click()
}